<template>
  <el-dialog
    :title="!dataForm.barginId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品ID" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品ID"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品库存" prop="goodsKucun">
      <el-input v-model="dataForm.goodsKucun" placeholder="商品库存"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="goodsImg">
      <el-input v-model="dataForm.goodsImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="jendTime">
      <el-input v-model="dataForm.jendTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="xiangouNumber">
      <el-input v-model="dataForm.xiangouNumber" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="商品低价" prop="goodsDijia">
      <el-input v-model="dataForm.goodsDijia" placeholder="商品低价"></el-input>
    </el-form-item>
    <el-form-item label="砍价人数" prop="barginRenshu">
      <el-input v-model="dataForm.barginRenshu" placeholder="砍价人数"></el-input>
    </el-form-item>
    <el-form-item label="砍价金额" prop="barginMoney">
      <el-input v-model="dataForm.barginMoney" placeholder="砍价金额"></el-input>
    </el-form-item>
    <el-form-item label="活动描述" prop="infomation">
      <el-input v-model="dataForm.infomation" placeholder="活动描述"></el-input>
    </el-form-item>
    <el-form-item label="活动状态" prop="huodongStatus">
      <el-input v-model="dataForm.huodongStatus" placeholder="活动状态"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则" prop="kanroots">
      <el-input v-model="dataForm.kanroots" placeholder="砍价规则"></el-input>
    </el-form-item>
    <el-form-item label="用户购买状态" prop="buystatus">
      <el-input v-model="dataForm.buystatus" placeholder="用户购买状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          barginId: 0,
          goodsId: '',
          goodsName: '',
          goodsPrice: '',
          goodsKucun: '',
          goodsImg: '',
          startTime: '',
          jendTime: '',
          xiangouNumber: '',
          goodsDijia: '',
          barginRenshu: '',
          barginMoney: '',
          infomation: '',
          huodongStatus: '',
          kanroots: '',
          buystatus: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品ID不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          goodsKucun: [
            { required: true, message: '商品库存不能为空', trigger: 'blur' }
          ],
          goodsImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          jendTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          xiangouNumber: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          goodsDijia: [
            { required: true, message: '商品低价不能为空', trigger: 'blur' }
          ],
          barginRenshu: [
            { required: true, message: '砍价人数不能为空', trigger: 'blur' }
          ],
          barginMoney: [
            { required: true, message: '砍价金额不能为空', trigger: 'blur' }
          ],
          infomation: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ],
          huodongStatus: [
            { required: true, message: '活动状态不能为空', trigger: 'blur' }
          ],
          kanroots: [
            { required: true, message: '砍价规则不能为空', trigger: 'blur' }
          ],
          buystatus: [
            { required: true, message: '用户购买状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.barginId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.barginId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargin/info/${this.dataForm.barginId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.bargin.goodsId
                this.dataForm.goodsName = data.bargin.goodsName
                this.dataForm.goodsPrice = data.bargin.goodsPrice
                this.dataForm.goodsKucun = data.bargin.goodsKucun
                this.dataForm.goodsImg = data.bargin.goodsImg
                this.dataForm.startTime = data.bargin.startTime
                this.dataForm.jendTime = data.bargin.jendTime
                this.dataForm.xiangouNumber = data.bargin.xiangouNumber
                this.dataForm.goodsDijia = data.bargin.goodsDijia
                this.dataForm.barginRenshu = data.bargin.barginRenshu
                this.dataForm.barginMoney = data.bargin.barginMoney
                this.dataForm.infomation = data.bargin.infomation
                this.dataForm.huodongStatus = data.bargin.huodongStatus
                this.dataForm.kanroots = data.bargin.kanroots
                this.dataForm.buystatus = data.bargin.buystatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargin/${!this.dataForm.barginId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'barginId': this.dataForm.barginId || undefined,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'goodsPrice': this.dataForm.goodsPrice,
                'goodsKucun': this.dataForm.goodsKucun,
                'goodsImg': this.dataForm.goodsImg,
                'startTime': this.dataForm.startTime,
                'jendTime': this.dataForm.jendTime,
                'xiangouNumber': this.dataForm.xiangouNumber,
                'goodsDijia': this.dataForm.goodsDijia,
                'barginRenshu': this.dataForm.barginRenshu,
                'barginMoney': this.dataForm.barginMoney,
                'infomation': this.dataForm.infomation,
                'huodongStatus': this.dataForm.huodongStatus,
                'kanroots': this.dataForm.kanroots,
                'buystatus': this.dataForm.buystatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
